using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using DelphiBinaryObjectParser;
using GameShield;
using System.Collections;

namespace Definition_Grabber
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("Usage: Grabber.exe <<Decrypted License>> <<Optional: -cr to ignore CodeRivets>>");
                //return;
            }

            DelphiBinaryObjectParser.DelphiParser parser = new DelphiBinaryObjectParser.DelphiParser();

            // get all defined Custom Parsers and add them to the parser object
            var q = from t in Assembly.GetExecutingAssembly().GetTypes()
                    where t.IsClass && t.BaseType.Name=="DelphiCustomParser"
                    select t;
            var l = q.ToList();
            foreach (var c in l)
            {
                DelphiCustomParser p = (DelphiCustomParser)Activator.CreateInstance(c);
                parser.addCustomParser(p);
            }

            DelphiObject o = parser.parseBinaryFile(args[0]);

            ArrayList list = (ArrayList)(o.getPropertyByName("TCList")).value;
            foreach (var ad in list)
            {
                AuthorizationDetails d = (AuthorizationDetails)ad;
                if (args.Length == 2 && args[1] == "-cr" && d.AuthType == "CODE RIVET")
                { }
                else
                {
                    Console.WriteLine("Auth #" + d.AuthID + ":");
                    PropertyInfo[] infos = d.GetType().GetProperties();
                    foreach (var i in infos)
                    {
                        var ob = i.GetValue(d, null);
                        if (i.Name != "AuthID")
                            Console.WriteLine("\t" + i.Name + " == " + ob.ToString());
                    }
                }
            }
            Console.Read();
        }
    }
}
